import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: {},
    login: true,
    cartIds: [] 
  },
  getters: {
  },
  mutations: {
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo;
    },
    setLogin(state, login) {
      state.login = login;
    },
   
    UPDATE_CART_IDS(state, ids) {
      state.cartIds = ids
      localStorage.setItem('cartIds', JSON.stringify(ids)) // 持久化存储
    },
    
    ADD_TO_CART(state, id) {
      if (!state.cartIds.includes(id)) {
        state.cartIds.push(id)
        localStorage.setItem('cartIds', JSON.stringify(state.cartIds))
      }
    },
    
    REMOVE_FROM_CART(state, id) {
      state.cartIds = state.cartIds.filter(courseId => courseId !== id)
      localStorage.setItem('cartIds', JSON.stringify(state.cartIds))
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [createPersistedState()]
})
